#pragma once

#include<iostream>
#include"Protocol.hpp"

class Calculator
{
public:
    Calculator()
    {}

    Response Execute(const Request &req)
    {
        //已经获得结构化的数据，进行计算
        Response resp;
        switch (req.Oper()) 
        {
            case '+':
                resp.SetResult(req.X() + req.Y());
                break;
            case '-':
                resp.SetResult(req.X() - req.Y());
                break;
            case '*':
                resp.SetResult(req.X() * req.Y());
                break;
            case '/':
                {
                    if(req.Y() == 0)
                    {
                        resp.SetCode(1);//除0错误
                    }
                    else 
                    {
                        resp.SetResult(req.X() / req.Y());
                    }
                }
                break;
            case '%':
                {
                    if(req.Y() == 0)
                    {
                        resp.SetCode(2);//模0错误
                    }
                    else 
                    {
                        resp.SetResult(req.X() % req.Y());
                    }
                }
                break; 
            default:
                resp.SetCode(3);//其他错误
                break;
        }
        return resp;
    }

    ~Calculator()
    {}
};